﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.Domain.ProductionWorking;
using MangoMes.Commont.Domain.SellStocks;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MediatR;
using MySql.Data.MySqlClient;

namespace MangoMes.Write.API.Applications.ProductionWrite
{
    public class StockRemovalCreateHandler : IRequestHandler<StockRemovalCreateCommand, ResultsDTO>
    {
        private readonly IBateRepository<Stockremoval> _stockremovalRepository;
        private readonly IBateRepository<SellStock> bate;
        private readonly IBateRepository<ProductsSale> productsSale;

        public StockRemovalCreateHandler(IBateRepository<Stockremoval> stockremovalRepository, IBateRepository<SellStock> bate, IBateRepository<ProductsSale> productsSale)
        {
            _stockremovalRepository = stockremovalRepository;
            this.bate = bate;
            this.productsSale = productsSale;
        }



        /// <summary>
        /// 出库单新增
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultsDTO> Handle(StockRemovalCreateCommand request, CancellationToken cancellationToken)
        {
            DapperHelper dapperHelper = new DapperHelper();
            ResultsDTO res = new ResultsDTO { Code = 0 };
            string dapper = dapperHelper.MangoMesWrite;
            using (MySqlConnection conn = new MySqlConnection(dapperHelper.RBACWrite))
            {
                conn.Open();
                using (MySqlTransaction tran = conn.BeginTransaction())
                {
                    try
                    {
                        SellStock sellStock = new SellStock();
                        string proInfo = $"UPDATE sellstock set SellStockStart=1  WHERE SellStockNo='{request.SellStockNo}'";
                        res.Code += await bate.Add(proInfo, sellStock, dapper);
                        ProductsSale produc = new ProductsSale();
                        string sq1 = $"select * FROM productssale WHERE SellStockNo='{request.SellStockNo}' and ProductsSaleNo=0";
                        var list = productsSale.GetAll(sq1, produc, dapper).ToList();
                        foreach (var item in list)
                        {
                            string sqlUpdate = $"UPDATE productssale SET ProductsSaleYesNo='1'";
                            res.Code += await productsSale.Add(sqlUpdate, item, dapper);

                            Stockremoval stockremoval = new Stockremoval();
                            string sqlInfo = $"SELECT * FROM stockremoval WHERE productManageNode='{item.ProductManageNode}'";
                            var info = _stockremovalRepository.GetAll(sqlInfo, stockremoval, dapper).FirstOrDefault();
                            info.StickWorkNum -= item.ProductsSaleNum;

                            string sqlUpdateThis= $"UPDATE stockremoval SET StickWorkNum={info.StickWorkNum} WHERE StockWorkId={info.StockWorkId}";
                            res.Code += await _stockremovalRepository.Add(sqlUpdateThis, stockremoval, dapper);
                        }
                        tran.Commit();
                    }
                    catch (Exception ex)
                    {
                        tran.Rollback();
                        res.Code = -1;
                        res.Message = ex.Message;
                    }
                }

            }
            return res;
        }
    }
}
